﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
using Investafinance;

namespace OutlookAddIn_Collaboratori
{
    partial class FormRegionCollaboratore
    {
        Outlook.ContactItem localContactItem; // wb 11.08.09
        public const string ADDRESS_TYPE_SMTP = "SMTP";

        #region Form Region Factory

        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass("IPM.Contact.Collaboratore")]
        [Microsoft.Office.Tools.Outlook.FormRegionName("OutlookAddIn_Collaboratori.FormRegionCollaboratore")]
        public partial class FormRegionCollaboratoreFactory
        {
            // Occurs before the form region is initialized.
            // To prevent the form region from appearing, set e.Cancel to true.
            // Use e.OutlookItem to get a reference to the current Outlook item.
            private void FormRegionCollaboratoreFactory_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
            {
            }
        }

        #endregion

        private string scon = @"Data Source=|DataDirectory|\Meta.sdf";   // строка конекта к бызе данных


        //будит хранить ((Outlook.TaskItem)this.OutlookItem)
        private Outlook.ContactItem CurrentItem;
        //Название таблиц
        private string nameTable = "collaboratori";
        private Investafinance.Sync.Shell.ShellFormRegion<Outlook.ContactItem> wFormRegion;


        // Occurs before the form region is displayed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        
        private void FormRegionCollaboratore_FormRegionShowing(object sender, System.EventArgs e)
        {
            //для того чтобы 100 раз не преобразовывать тип сделаем это 1 раз
            CurrentItem = ((Outlook.ContactItem)this.OutlookItem);

            Investafinance.Sync.Shell.ShellFieldItem<Outlook.ContactItem> CollectionField = new Investafinance.Sync.Shell.ShellFieldItem<Outlook.ContactItem>(CurrentItem,ThisAddIn.associateMap);
            wFormRegion = new Investafinance.Sync.Shell.ShellFormRegion<Outlook.ContactItem>(CurrentItem, scon, nameTable, ref CollectionField);


            // wb 11.08.09
            localContactItem = this.OutlookItem as Outlook.ContactItem;
            if (localContactItem != null)
            {
                this.textBoxLogin.Text     = localContactItem.OrganizationalIDNumber; // учетная запись (логин)
                this.textBoxPassword.Text  = localContactItem.GovernmentIDNumber;     // пароль
                if (ADDRESS_TYPE_SMTP.Equals(localContactItem.Email1AddressType))
                    this.textBoxEmail1.Text    = localContactItem.Email1Address; // почта на ctradeorg
                else
                    this.textBoxEmail1.Text = localContactItem.Email1DisplayName;
                this.textBoxLogin1.Text    = localContactItem.NickName;      // логин
                this.textBoxPassword1.Text = localContactItem.User2;         // пароль
                if (ADDRESS_TYPE_SMTP.Equals(localContactItem.Email2AddressType))
                    this.textBoxEmail2.Text = localContactItem.Email2Address; // почта на investa
                else
                    this.textBoxEmail2.Text = localContactItem.Email2DisplayName;
                this.textBoxLogin2.Text    = localContactItem.User3;         // логин
                this.textBoxPassword2.Text = localContactItem.User4;         // пароль

                this.textBox1.Text = localContactItem.UserProperties[1].Value.ToString();

                this.textBoxLogin.TextChanged     += new EventHandler(this.textBoxLogin_TextChanged);
                this.textBoxPassword.TextChanged  += new EventHandler(this.textBoxPassword_TextChanged);
                this.textBoxEmail1.TextChanged    += new EventHandler(this.textBoxEmail1_TextChanged);
                this.textBoxLogin1.TextChanged    += new EventHandler(this.textBoxLogin1_TextChanged);
                this.textBoxPassword1.TextChanged += new EventHandler(this.textBoxPassword1_TextChanged);
                this.textBoxEmail2.TextChanged    += new EventHandler(this.textBoxEmail2_TextChanged);
                this.textBoxLogin2.TextChanged    += new EventHandler(this.textBoxLogin2_TextChanged);
                this.textBoxPassword2.TextChanged += new EventHandler(this.textBoxPassword2_TextChanged);

                localContactItem.PropertyChange += new Microsoft.Office.Interop.Outlook.ItemEvents_10_PropertyChangeEventHandler(localContactItem_PropertyChange);
            }
        }
        private void Inspectors_NewInspector(Outlook.Inspector Inspector)
        {
            try
            {
                // OutlookItem olItem = new 
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
            }
        }

        void localContactItem_PropertyChange(string Name)
        {
            if (Name.Equals("Email1Address"))
            {
                if (ADDRESS_TYPE_SMTP.Equals(localContactItem.Email1AddressType))
                    this.textBoxEmail1.Text = localContactItem.Email1Address;
                else
                    this.textBoxEmail1.Text = localContactItem.Email1DisplayName;
                return;
            }
            if (Name.Equals("Email2Address"))
            {
                if (ADDRESS_TYPE_SMTP.Equals(localContactItem.Email2AddressType))
                    this.textBoxEmail2.Text = localContactItem.Email2Address;
                else
                    this.textBoxEmail2.Text = localContactItem.Email2DisplayName;
                return;
            }
        }
        // Occurs when the form region is closed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void FormRegionCollaboratore_FormRegionClosed(object sender, System.EventArgs e)
        {
            wFormRegion.Close();
        }

        // wb 12.08.09 срабатывает перед первым показом формы
        private void FormRegionCollaboratore_Load(object sender, System.EventArgs e)
        {
        }
        // wb 17.08.09 меняем содержимое outlookItem после редактирования пользователем ++>
        private void textBoxLogin_TextChanged(object sender, EventArgs e)
        {
            if (localContactItem != null)
            {
                //localContactItem.OrganizationalIDNumber = this.textBoxLogin.Text;     // учетная запись (логин)
                wFormRegion.CollectionField["OrganizationalIDNumber", "OrganizationalIDNumber"] = this.textBoxLogin.Text;
            }
        }
        private void textBoxPassword_TextChanged(object sender, EventArgs e)
        {
            if (localContactItem != null)
            {
                //localContactItem.GovernmentIDNumber = this.textBoxPassword.Text;   // пароль
                wFormRegion.CollectionField["GovernmentIDNumber", "GovernmentIDNumber"] = this.textBoxPassword.Text;
            }
        }
        private void textBoxEmail1_TextChanged(object sender, EventArgs e)
        {
            if (localContactItem != null)
            {
                //localContactItem.Email1Address = this.textBoxEmail1.Text;    // почта на ctradeorg
                wFormRegion.CollectionField["Email", "Email1Address"] = this.textBoxEmail1.Text;
                localContactItem.Email1AddressType = ADDRESS_TYPE_SMTP;
            }
        }
        private void textBoxLogin1_TextChanged(object sender, EventArgs e)
        {
            if (localContactItem != null)
            {
                //localContactItem.NickName = this.textBoxLogin1.Text;            // логин
                wFormRegion.CollectionField["Nickname", "NickName"] = this.textBoxLogin1.Text;
            }
        }
        private void textBoxPassword1_TextChanged(object sender, EventArgs e)
        {
            if (localContactItem != null)
            {
                // localContactItem.User2 = this.textBoxPassword1.Text;         // пароль
                wFormRegion.CollectionField["UserField2", "User2"] = this.textBoxPassword1.Text;
            }
        }
        private void textBoxEmail2_TextChanged(object sender, EventArgs e)
        {
            if (localContactItem != null)
            {
                // localContactItem.Email2Address = this.textBoxEmail2.Text;    // почта на investa
                wFormRegion.CollectionField["Email2", "Email2Address"] = this.textBoxEmail2.Text;
                localContactItem.Email1AddressType = ADDRESS_TYPE_SMTP;
            }
        }
        private void textBoxLogin2_TextChanged(object sender, EventArgs e)
        {
            if (localContactItem != null)
            {
                //localContactItem.User3 = this.textBoxLogin2.Text;            // логин
                wFormRegion.CollectionField["UserField3", "User3"] = this.textBoxLogin2.Text;
            }
        }
        private void textBoxPassword2_TextChanged(object sender, EventArgs e)
        {
            if (localContactItem != null)
            {
                //localContactItem.User4 = this.textBoxPassword2.Text;         // пароль
                wFormRegion.CollectionField["UserField4", "User4"] = this.textBoxPassword2.Text;
            }
        }

        private void textBoxLogin_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (localContactItem != null)
            {
                //Тест
                wFormRegion.CollectionField["_x0442__x0435__x0441__x0442__x00", "UserProperties[my]"] = this.textBox1.Text;
            }

        }
        // <++

    }
}
